﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using Lib.Utils;
using System.Text;
using System.Windows.Forms;

namespace MS.INV
{
    public partial class MSystem : DevExpress.XtraEditors.XtraForm
    {
        public MSystem()
        {
            InitializeComponent();
        }

        private void MSystem_Load(object sender, EventArgs e)
        {
            try
            {
                Lib.Bases.Admin.AdOption.IsBasic = true;

                lblLocalUpdatePathContent.Text = Systems.Options.LocalUpdatePath;
                chkAutoUpdate.Checked = Systems.Options.AutoUpdateFromInternet;
                using (Systems.ZZ_System sys = new Systems.ZZ_System())
                {
                    sys.LoadData(1);
                    lblUpdatePath.Text = (Is.IsNullOrEmpty(sys.Agr1)) ? "" : Uti.GetShortPathName(sys.Agr1);
                    string _hour = sys.Agr2 + ":" + sys.Agr3;
                    string _days = sys.Agr4;
                    if (_days.Trim() == "")
                        lblBackupSchedule.Text = "Không kích hoạt.";
                    else
                        lblBackupSchedule.Text = RepDays(_days) + "    " + _hour;
                }

                lblMachineNumber.Text = Lib.Data.ConnInfo.Max_Computers.ToString();
                lblCopyright.Text = (Lib.Data.ConnInfo.Max_Rows == 0) ? "Bản quyền" : "Miễn phí";

                if (!Is.IsNullOrEmpty(Systems.Options.KeyCode))
                    lblKeyCode.Text = Systems.Options.KeyCode.Substring(0, 4) + " - " + Systems.Options.KeyCode.Substring(4, 4);
                else
                    lblKeyCode.Text = "0000 - 0000";

                lblDV.Font = new Font(lblDV.Font.FontFamily, 10, FontStyle.Bold);
                lblTenDV.Font = new Font(lblDV.Font.FontFamily, 12, FontStyle.Bold);

                lblTenDV.Text = Systems.Options.TenQuan;
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Form load");
            }
        }

        private string RepDays(string days)
        {
            string res = days.ToLower();
            res = res.Replace("monday", " T2");
            res = res.Replace("tuesday", " T3");
            res = res.Replace("wednesday", " T4");
            res = res.Replace("thursday", " T5");
            res = res.Replace("friday", " T6");
            res = res.Replace("saturday", " T7");
            res = res.Replace("sunday", " CN");

            return res;
        }

        private void lbAdmin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Lib.Bases.Admin.UserManager us = new Lib.Bases.Admin.UserManager();
            us.StartPosition = FormStartPosition.CenterScreen;
            us.ShowDialog();
        }

        private void lblChangePass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Lib.Bases.Admin.ChangePassword ff = new Lib.Bases.Admin.ChangePassword();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
        }

        private void lblData_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Lib.Bases.DB.BkData ff = new Lib.Bases.DB.BkData();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
        }

        private void lblImport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Lib.Bases.DB.Import ff = new Lib.Bases.DB.Import();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
        }

        private void btReg_Click(object sender, EventArgs e)
        {
            Lib.Lic.RegInfo ff = new Lib.Lic.RegInfo();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
        }

        private void btEnterKey_Click(object sender, EventArgs e)
        {
            Lib.Lic.RegCode ff = new Lib.Lic.RegCode();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
        }

        private void lblSmartPhone_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("Monitor.exe");
            }
            catch { }
        }

        private void lblLocalUpdatePath_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            txtLocalUpdatePath.Text = lblLocalUpdatePathContent.Text;
            ShowLocalUpdatePath(true);
            lblLocalUpdatePathContent.Visible = false;
        }

        private void ShowLocalUpdatePath(bool value)
        {
            txtLocalUpdatePath.Visible = value;
            btLocalUpdatePath.Visible = value;
        }

        private void btLocalUpdatePath_Click(object sender, EventArgs e)
        {
            using (Systems.ZZ_System sys = new Systems.ZZ_System())
                sys.ArgUpdate(5, "Agr4", txtLocalUpdatePath.Text);

            lblLocalUpdatePathContent.Text = txtLocalUpdatePath.Text;
            ShowLocalUpdatePath(false);
            lblLocalUpdatePathContent.Visible = true;
        }

        private void chkAutoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            using (Systems.ZZ_System sys = new Systems.ZZ_System())
                sys.ArgUpdate(5, "Agr5", chkAutoUpdate.Checked.ToString());
        }

        private void btSupp_Click(object sender, EventArgs e)
        {
            Lib.Com.SupportOnl ff = new Lib.Com.SupportOnl();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.Show();
        }

        private void btHelp_Click(object sender, EventArgs e)
        {
            Uti.RunFile(@"Helps\Helps.exe");
        }

        private void btOwner_Click(object sender, EventArgs e)
        {
            Systems.SetOwner ff = new Systems.SetOwner();
            ff.ShowDialog();
            if (ff.IsChange)
            {
                lblTenDV.Text = Systems.Options.TenQuan;
            }
        }

        private void lblKeyCode_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                Systems.WebComClient wcom = new Systems.WebComClient();

                Lib.Com.WebCom com = new Lib.Com.WebCom();
                com.QueryDone += new Lib.Com.WebCom.QueryDoneHandler(com_QueryDone);
                com.Query(wcom.QueryResult);
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, "WebCom class", "Communication to Web");
            }
            finally { this.Cursor = Cursors.Default; }
        }

        void com_QueryDone(Lib.Com.WebCom.QueryResult result)
        {
            if (!Lib.Utils.Is.IsNullOrEmpty(result.Error) && result.Error.Length > 0)
            {
                Lib.Bases.SysError.RaiseError(new Exception(result.Error), "WebCom class", "Communication to Web");
            }
            Lib.Bases.Msg.InforOK("Đã kiểm tra thành công cho kết nối từ xa");
        }
    }
}
